package com.allwees.bs.c.module.user.req;

import com.allwees.bs.core.model.constant.EPromotionChannel;
import com.allwees.bs.core.modelbase.constant.ErrorMessage;
import com.allwees.bs.core.modelbase.req.SignatureException;
import com.allwees.core.common.constant.EPlatform;
import com.allwees.core.common.util.encrypt.MD5Util;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Data
@EqualsAndHashCode(callSuper = true)
public class VisitorShippingAddressReq extends ShippingAddressReq {

    private String signature;

    private Date time;

    @NotNull(message = ErrorMessage.PARAM_NOT_NULL)
    private EPlatform platform;

    @NotBlank(message = ErrorMessage.PARAM_NOT_NULL)
    private String email;

    private String shareCode;

    private EPromotionChannel inviteChannel;

    private String signSeparator() {
        return platform.isApp() ? "_APP_" : "_WEB_";
    }

    private Object[] signValues() {
        return new Object[]{email, firstName, lastName, phone, getTime().getTime()};
    }

    final public void validateSignature() {
        String signText = StringUtils.join(signValues(), signSeparator());
        if(!signature.equalsIgnoreCase(MD5Util.digest(signText))) {
            throw new SignatureException();
        }
    }
}
